package cn.xueden.system.controller;

import cn.xueden.common.result.Result;
import cn.xueden.system.model.entity.Role;
import cn.xueden.system.service.IRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**功能描述：角色控制类
 * @author:梁志杰
 * @date:2025/6/10
 * @description:cn.xueden.system.controller
 * @version:1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/role")
public class RoleController {

    private final IRoleService roleService;

    /**
     * 分页获取角色信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/page")
    @PreAuthorize("hasAnyAuthority('role_admin')")
    public Result<IPage<Role>> getPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize){
        Page<Role> page = new Page<>(pageNum, pageSize);
        return Result.success("查询成功",roleService.getPage(page));
    }

    /**
     * 新增角色
     * */
    @PostMapping
    @PreAuthorize("hasAnyAuthority('role_admin')")
    public Result<Void> addRole(@RequestBody Role role) {
        boolean  result = roleService.createRole(role);
        return result? Result.success("新增成功"):Result.failed("新增失败");
    }

    /**
     * 修改角色
     * */
    @PutMapping
    @PreAuthorize("hasAnyAuthority('role_admin')")
    public Result<Void> updateRole(@RequestBody Role role) {
        boolean  result =  roleService.updateRole(role);
        return result?Result.success("修改成功"):Result.failed("修改失败");
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('role_admin')")
    public Result<Void> deleteRole(@PathVariable Integer id) {
       boolean  result = roleService.deleteRole(id);
        return result?Result.success("删除成功"):Result.failed("删除失败");
    }
}
